function varargout = analySRQuality(X, Y, scale, netname, hSR, solver_mode)
% [psnrs, ssims, ImgList] = sranaly.analySRQuality(ImgGroundTruth, scale, netname, hSR, solver_mode)
% Input:
%   -- ImgGroundTruth: Ground truth image tensor/list or image path
% Output:
%   -- psnrs/ssims(:, 1) save PSNR/SSIM values for Bicubic method
%   -- psnrs/ssims(:, 2) save PSNR/SSIM values for Advanced (eg., CNN) method
%   -- ImgList{i, :} save {y0, x, x, yhat}
%       y0: ground truth
%       x:  low resolution image
%       x: bicubic reconstruction result
%       yhat:  CNN reconstruction result

% global bLoadSRResult
% bLoadSRResult = true;
% if isempty(bLoadSRResult), bLoadSRResult = false; end

if nargin < 5, hSR = []; end
if nargin < 6, solver_mode = []; end

%%
netname = strtrim(netname);
shift = sranaly.get_shift(netname, scale);

NetNameList = strsplit(netname, '->');
netname = NetNameList{end};

%%
bOnImgSet = ischar(X) && isempty(Y);

if bOnImgSet
    [Y, ImgSetName] = srdata.loadImgSet(X);
    
    [bFound, ~, SRMatFile] = myinput.searchDataByParam(srpath.getSRImgSetHome, 'st-*.mat', [ImgSetName '-*.mat'], netname, ImgSetName, scale);
    if bFound
        st = load(SRMatFile);
        varargout = {st.psnrs, st.ssims, st.Y, st.Yhat};
        fprintf('\t%s: mean(PSNR/SSIM) = %.2f/%.4f\n', netname, mean(st.psnrs), mean(st.ssims));
        return
    end
    
    [X, Y] = getLRImg(netname, scale, Y);
end

[netname, hSR] = get_net(bOnImgSet, netname, hSR, scale);

%% Perform SR
mycaffe.init(solver_mode, netname);
Yhat = hSR(X);

%% Calc PSNR/SSIM
% netname = 'FSRCNN'; scale = 3; shift = [0, 1];
if iscell(X)
    ImgNum = length(X);
else
    ImgNum = size(X, 3);
end
psnrs = zeros(ImgNum, 1); ssims = psnrs; R = cell(ImgNum, 1);
for i = 1 : ImgNum
    [psnrs(i), R{i}] = srimg.psnr(Y{i}, Yhat{i}, scale, shift);
    ssims(i) = srimg.ssim(Y{i}, Yhat{i}, scale, shift);
    
    fprintf('\t%s: Image %g/%g, PSNR/SSIM = %.2f/%.4f\n', netname, i, ImgNum, psnrs(i), ssims(i));
end

% fprintf('\t===================================\n');
% fprintf('\t%s: mean(PSNR/SSIM) = %.2f/%.4f\n', netname, mean(psnrs), mean(ssims));

%%
if ~bOnImgSet
    varargout = {psnrs, ssims, Yhat, R};
    return
end

varargout = {psnrs, ssims, Y, Yhat};
save(SRMatFile, 'psnrs', 'ssims', 'Y', 'Yhat');

function [netname, hSR] = get_net(bOnImgSet, netname, hSR, scale)
if bOnImgSet
    if strcmpi(netname, 'Bicubic')
        hSR = @(X) X;
    else
        hSR = srmodel.getDeployFunc(netname, scale);
    end
    return
end

bForceSR = netname(1) == '!';
if isempty(hSR) && ~bForceSR
    hSR = @(X) X;
    return
end

if bForceSR
    netname(1) = [];
    hSR = srmodel.getDeployFunc(netname, scale);
end

function [X, Y] = getLRImg(netname, scale, Y)
%%
startingList = 'Bicubic->Bicubic';
if str(netname).isbeginwith('FSRCNN')
    startingList = 'Bicubic->I';
end

startingList = strsplit(startingList, '->');

%%
[X, Y] = cellfun(@(y)get_lr_img(y, scale, startingList), Y, 'UniformOutput', false);

function [x, y] = get_lr_img(y, scale, starting)
y = srimg.modcrop(y, scale);
x = srimg.im2lr(y, scale, starting{1}, starting{2});
